﻿using Cemit.NetFramework.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Cemit.PolyProto;
using Cemit.Awaiter;

namespace Cemit.NetFramework
{
    partial class PolyNetwork
    {
        private static readonly MainClient mainClient = new MainClient();

        /// <summary>使用一个IP和端口连接到服务器</summary>
        public static async Task<AsyncReport> Connect(string ip, int port)
        {
            Settings.ip = ip;
            Settings.port = port;

            return await ConnectWithSettings();
        }

        /// <summary>使用一个 <see cref="PolyServerSettings"/> 连接到服务器</summary>
        public static async Task<AsyncReport> Connect(PolyServerSettings settings)
        {
            Settings = settings;

            return await ConnectWithSettings();
        }

        /// <summary>使用配置连接服务器</summary>
        private static async Task<AsyncReport> ConnectWithSettings()
        {
            Debug.LogInfo($"尝试连接主服务器: {Settings.ip}:{Settings.port}");
            
            var report = await mainClient.Connect(Settings.ip, Settings.port);

            if (report.isSucceed)
            {
                Debug.LogInfo($"连接主服务器成功！");
            }
            else
            {
                Debug.LogInfo($"连接主服务器失败: {report.output}");
            }

            return report;
        }

        /// <summary>获取大厅列表</summary>
        public static async Task<IEnumerable<LobbyInfo>> GetLobbyList()
        {
            Debug.LogInfo($"尝试获取大厅列表: {Settings.ip}:{Settings.port}");

            var report = await mainClient.GetLobbyList();

            if (report is null)
            {
                Debug.LogWarning($"获取大厅列表失败！");
            }
            else
            {
                Debug.LogInfo($"获取大厅列表成功！");
            }

            return report;
        }
    }
}
